<?php

namespace app\api\controller;

use think\Controller;
use think\Cache;
use think\Db;
class Shop extends Common
{

	//矿机商城列表
	public function index()
	{

		$data = db('shop_project')->field('id, name, price,  yxzq, stock,capacity, imagepath , limit,kjsl')->where('zt','1')->order("id asc")->select();
		$this->assign('data', $data);
		foreach($data as $k => &$v){
			$v['imagepath'] = 'http://'.$_SERVER['SERVER_NAME'].$v['imagepath'];
		}
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}
	//矿机详情
	public function jiaoyi()
	{
		$id =  input('id') ? input('id') : apifasong(['zt' => -1, 'msg' => '缺少参数']);
		$row = db('shop_project')->field(true)->find($id);
		if (!$row) apifasong(['zt' => -1, 'msg' => '无该类型矿机']);
	
		$data = db('shop_project')->field('price, content, name, imagepath')->where(array('id' => $id))->find();
		$data['imagepath'] = $_SERVER['SERVER_NAME'].$data['imagepath'];
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}
	// 购买矿车
	public function confirmproject()
	{	
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$id =  (input('id')) ? input('id') : apifasong(['zt' => -1, 'msg' => '缺少参数']);
		input('secpwd')? input('secpwd') : apifasong(['zt' => -1, 'msg' => '缺少参数']);

		if ($userinfo['secpwd'] <> MD5($appdata['secpwd'])) apifasong(['zt' => -1, 'msg' => "支付密码错误"]);
		$orderSn = orderSn(); //订单生成
		

		$project = db('shop_project')->where(array('id' => $id))->find();

		if (!$project) apifasong(['zt' => -1, 'msg' => '无该类型矿机']);
		if ($project['stock'] < 1) apifasong(['zt' => -1, 'msg' => '该类型矿机已售空']); 

		//限购
		$order_count = db('shop_orderform')->where(array('mobile' => $userinfo['uname'], 'project' => $project['name'], 'type' => array('neq', 2), 'zt' => array('neq', 2)))->count();

		if ($order_count >= $project['limit']) {
			apifasong(['zt' => -1, 'msg' => $project['name'] . "最多购买" . $project['limit'] . "台"]);
		}
		$price = $project['price'];

		if ($userinfo['idc'] < $price) {
			apifasong(['zt' => -1, 'msg' => '你的账户余额不足']);
		} else {
			// 启动事务
			Db::startTrans();
			try{

				db('user')->where(array('uname' => $userinfo['uname']))->setDec('idc', $price);


				if($userinfo['kjzt'] != 1){
					db('user')->where(array('uname' => $userinfo['uname']))->data(array('kjzt' => 1))->update();
				}


				db('shop_project')->where(array('id' => $id))->setDec('stock', 1);

				$orderform = db('shop_orderform');
				$map['mobile'] = $userinfo['uname']; // 用户手机号
				$map['uid'] = $userinfo['id']; // 用户ID
				$map['project'] = $project['name']; // 矿机名称
				$map['yxzq'] = $project['yxzq']; //运行周期（天）
				$map['price'] = $price;  // 矿机价格
				$map['addtime'] = time();
				$map['imagepath'] = $project['imagepath']; // 图片
				$map['capacity']= $project['capacity']; // 产量总币
				$map['type']	= 0; // 用户购买
				$map['kjsl'] = $project['kjsl'];// 算力
				$map['kjbh'] = $orderSn; // 订单
				
				$map['kj_type'] = $id;
				$id = $orderform->insertGetId($map);
				$logocoin["uname"] = $userinfo['uname']; // 用户名
				$logocoin["uid"] = $userinfo['id']; // 金幣
				$logocoin["type"] = 5;
				$logocoin["mqian"] = $userinfo['idc'];//当前帐户金币余额
				$logocoin["money"] = '-'.$price;//变动数量
				$logocoin["mhou"] = $userinfo['idc'] - $price;//变动之后的
				$logocoin["note"] = '购买矿机';
				$logocoin["order_id"] = $id;
				$logocoin["order_sn"] = $orderSn;
				$logocoin["time"] = time();
				db('logocoin')->insert($logocoin);
			// 提交事务
			Db::commit();
			$userdata = db('user')->field(true)->where(['uname' => $userinfo['uname']])->find();
			apifasong(['zt' => 1, 'msg' => '购买成功，已加入到我的矿机收益列表','userinfo' => apiuserbaseinfo($userdata)]);
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			apifasong(['zt' => -1, 'msg' => '购买失败，请重新操作']);
		}

		}
	}


	
}
